import Vue from 'vue'
import Router from 'vue-router'
import register from '@/view/register'
import login from '@/view/Login'
import userinfo from '@/view/userInfo'
import edit from '@/view/Edit'
import home from '@/view/Home'
import article from '@/view/Article'

Vue.use(Router)


const  routes= [
    {
      path: '/register',
      name: 'register',
      component: register
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/userinfo',
      name: 'userinfo',
      component: userinfo,
      meta:{
        istoken:true
      }
    },
  {
    path: '/edit',
    name: 'edit',
    component: edit,
    meta:{
      istoken:true
    }
  },
  {
    path: '/',
    name: 'home',
    component: home,
    //保持页面不会被销毁
    meta:{
      keepalive:true
    }
  },
  {
    path: '/article/:id',
    name: 'article',
    component: article,
  }
  ]
// 创建router对路由进行管理，由构造函数 new Router()创建，接收routes参数
const router = new Router({
  routes: routes // 简写：routes
});
router.beforeEach((to,from,next) => {
  if (!localStorage.getItem('token') && !localStorage.getItem('id') && to.meta.istoken == true){
    router.push('/login')
    Vue.prototype.$msg.fail("请重新登录")
  }
  next();
})
export default router
